import path from 'node:path';

import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { defineConfig } from 'vite';
import qiankun from 'vite-plugin-qiankun';

export default defineConfig({
  resolve: {
    dedupe: ['vue', 'vue-router', '@vue/shared'],
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@tuxing/shared-components': path.resolve(__dirname, '../../packages/shared-components/src'),
      '@tuxing/network-http': path.resolve(__dirname, '../../packages/http/src'),
      '@tuxing/shared-store': path.resolve(__dirname, '../../packages/shared-store/src'),
      '@tuxing/shared-utils': path.resolve(__dirname, '../../packages/shared-utils/src'),
    },
  },

  server: {
    port: 3007,
    host: '0.0.0.0',
    cors: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control',
    },
    fs: {
      allow: ['..', '../..', '../../..'],
    },
    proxy: {
      '/dev-api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        // 不重写路径，保留 /dev-api 前缀
        rewrite: (path) => path
      }
    }
  },

  // 微前端配置 - 开发和生产都使用相对路径
  base: './',

  plugins: [
    vue(),
    vueJsx(),
    qiankun('dashboard-app', {
      useDevMode: true,
    }),
  ],

  optimizeDeps: {},

  build: {
    lib: {
      entry: './src/main.ts',
      name: 'dashboardApp',
      formats: ['umd'],
      fileName: 'dashboardApp',
    },
    rollupOptions: {
      external: ['vue', 'vue-router', 'pinia'],
      output: {
        globals: {
          vue: 'Vue',
          'vue-router': 'VueRouter',
          pinia: 'Pinia',
        },
      },
    },
  },

  define: {
    __VUE_OPTIONS_API__: true,
    __VUE_PROD_DEVTOOLS__: false,
  },
});
